/**
 * 
 */
package com.zui.util.excel;

import org.apache.poi.hssf.usermodel.HSSFCell;

/**
 * @author 刘洪 2010-3-5
 * 
 * 
 */
public class XLSCellReader {

	/**
	 * 获取整型数据
	 * 
	 * @param cell
	 * @return int
	 */
	public static int getInt(HSSFCell cell) {
		int data = 0;
		try {
			data = Integer.parseInt(getCellData(cell));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 获取浮点型数据
	 * 
	 * @param cell
	 * @return float
	 */
	public static float getFloat(HSSFCell cell) {
		float data = 0;
		try {
			data = Float.parseFloat(getCellData(cell));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 获取字符型数据
	 * 
	 * @param cell
	 * @return String
	 */
	public static String getString(HSSFCell cell) {
		String data = "";
		data = getCellData(cell);
		return data;
	}

	private static String getCellData(HSSFCell cell) {
		String data = "";
		String t = "";
		if (null != cell) {
			switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_NUMERIC:
				try {
					t = String.valueOf(cell.getNumericCellValue());
					t = t.substring(0, t.lastIndexOf("."));
				} catch (Exception e) {
					e.printStackTrace();
				}
				data = t;
				break;
			case HSSFCell.CELL_TYPE_STRING:
				try {
					data = cell.getStringCellValue();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				data = "";
				break;
			}
		}
		return data;
	}
}
